package com.liu.base.controller;

import com.liu.base.pojo.Label;
import com.liu.base.service.LabelService;
import com.liu.common.entity.PageResult;
import com.liu.common.entity.Result;
import com.liu.common.status.CodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {


    @Autowired
    private LabelService labelService;

    /**
     * 查询全部列表
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){

        return  new Result(true, CodeEnum.OK.getCode(),"查询成功",labelService.findAll());
    }

    /**
     * 根据ID查询标签
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable String id){
          return  new Result(true,CodeEnum.OK.getCode(),"查询成功",labelService.findById(id));
    }

    /**
     * 新增标签
     * @param label
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Label label){
          labelService.add(label);
          return  new Result(true,CodeEnum.OK.getCode(),"新增成功");
    }

    /**
     * 修改标签
     * @param id
     * @param label
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable String id ,@RequestBody Label label){
        labelService.update(label);
        return  new Result(true,CodeEnum.OK.getCode(),"更新成功");
    }

    @RequestMapping(value="/{id}" ,method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String id){
        labelService.deleteById(id);
        return new Result(true,CodeEnum.OK.getCode(),"删除成功");
    }

    @RequestMapping(value="/search" ,method = RequestMethod.POST)
    public Result search(@RequestBody Label label){
        List<Label> results= labelService.search(label);
        return new Result(true,CodeEnum.OK.getCode(),"查询成功",results);
    }


    @RequestMapping(value="/search/{page}/{size}" ,method = RequestMethod.POST)
    public Result search(@RequestBody Label label,@PathVariable int page,@PathVariable int size){
        Page<Label> searchPage= labelService.searchPage(label,page,size);
        return new Result(true,CodeEnum.OK.getCode(),"查询成功",new PageResult<Label>(searchPage.getTotalElements(),searchPage.getContent()));
    }
}
